For the Redemption project, I will be focusing on Projects 1 and 3. To begin, Project 3 involves using an API to obtain a dataframe. I reviewed Project 4 to understand how the lecturer wrote the code to retrieve YouTube data. Inspired by this, I researched meme generator websites and discovered how to use their API.
The API from this website returns 100 memes, ordered by the number of times they were captioned in the last 30 days.
I utilized Google Sheets to create a script that calls the API and imports the relevant data into the sheets, then publishes it as a CSV. Below is the code to import the API data into Google Sheets:
function fetchData() {
// Replace with your API URL
const apiUrl = 'https://api.imgflip.com/get_memes';
// Fetch the JSON data from the API
const response = UrlFetchApp.fetch(apiUrl);
const json = response.getContentText();
const data = JSON.parse(json);
// Get the 'memes' array from the JSON data
const memes = data.data.memes;
// Get the active sheet
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Set headers
const headers = ['ID', 'Name', 'URL', 'Width', 'Height', 'Box Count', 'Captions'];
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
// Set meme data
const memeData = memes.map(meme => [
meme.id,
meme.name,
meme.url,
meme.width,
meme.height,
meme.box_count,
meme.captions
]);
sheet.getRange(2, 1, memeData.length, headers.length).setValues(memeData);
}
With the data in hand, we can proceed with the animations. For this project, I will create a GIF for the thumbnail preview and a table displaying the top 10 most popular memes from the past month. I will not perform any in-depth analysis of the memes, as the only numerical data available is the number of captions. Instead, I will categorize the memes based on their popularity over the last 30 days.
# Import all the nessary libraries
library(tidyverse)
library(dplyr)
library(jsonlite)
library(magick)
library(kableExtra)
# Load the data
memes <- data.frame(read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vQll5ONs_D5XyKMbfrNEFQRwy05VjHZ9qdVeA-wcRJZYrwq5_N4NsdJkHKhozOe3ns3xAscLzoO3zR6/pub?gid=0&single=true&output=csv"))
# view(memes)
# Getting the URL while ignoring the NA values
thumbnail <- memes$URL %>% na.omit()
# Creating the GIF
image_read(thumbnail) %>%
image_join() %>%
image_scale(700) %>%
image_animate(fps = 1) %>%
image_write("thumbnail.gif")
# Mutate the memes into popular and not popular based on if the memes excees the mean Captions
memes <- memes %>%
mutate(popular = ifelse(Captions >= mean(Captions),
"Popular",
"Not Popular"
))
# Filter the popular memes and arrange them in descending order
popular <- memes %>%
filter(popular == "Popular") %>%
arrange(desc(Captions))
# Display the top 10 popular memes
top_popular_memes <- head(popular, 10)
# Display the top 10 popular memes in a table using a new libary called kableExtra
# top_popular_memes %>%
# select(Name, URL, Captions) %>%
# mutate(Preview = paste0('<img src="', URL, '" width="100" />')) %>%
# select(Name, Preview, Captions) %>%
# kable(escape = FALSE, format = "html", col.names = c("Meme Name", "Preview", "Captions")) %>%
# kable_styling(full_width = FALSE, position = "center")
# top_popular_memes
top_popular_memes %>%
select(Name, URL, Captions) %>%
mutate(Preview = paste0('<img src="', URL, '" width="100" />')) %>%
select(Name, Preview, Captions) %>%
knitr::kable(escape = FALSE, format = "html", col.names = c("Meme Name", "Preview", "Captions")) %>%
kable_styling(full_width = FALSE, position = "center")
top_popular_memes %>%
select(Name, URL, Captions) %>%
mutate(Preview = paste0('<img src="', URL, '" width="100" />')) %>%
select(Name, Preview, Captions) %>%
knitr::kable(escape = FALSE, format = "html", col.names = c("Meme Name", "Preview", "Captions")) %>%
kable_styling(full_width = FALSE)
| Meme Name | Preview | Captions |
|---|---|---|
| Drake Hotline Bling |
|
1246250 |
| Distracted Boyfriend |
|
1025000 |
| Two Buttons |
|
996500 |
| Left Exit 12 Off Ramp |
|
632750 |
| Change My Mind |
|
615000 |
| Batman Slapping Robin |
|
609750 |
| UNO Draw 25 Cards |
|
554750 |
| Running Away Balloon |
|
529750 |
| One Does Not Simply |
|
450000 |
| Expanding Brain |
|
435750 |